package com.ysd.modules.sys.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ysd.common.exception.RRException;
import com.ysd.common.utils.R;
import com.ysd.modules.sys.entity.CourseEntity;
import com.ysd.modules.sys.entity.SysUserEntity;
import com.ysd.modules.sys.entity.Topic;
import com.ysd.modules.sys.model.StatusReqs;
import com.ysd.modules.sys.model.TopicModel;
import com.ysd.modules.sys.model.TopicResp;
import com.ysd.modules.sys.service.TopicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dapeng
 * @since 2021-07-15
 */
@RestController
@RequestMapping("/topic")
public class TopicController extends AbstractController {

    @Autowired
    private TopicService topicService;

    /**
     * 列表查询
     * @param req
     * @return R
     */
    @GetMapping("/list")
    @RequiresPermissions("topic:list")
    public R listSearch(@RequestParam Map<String, Object> req){
        IPage<TopicResp> page = topicService.listSearch(req);
        R ok = R.ok();
        ok.put("data", page.getRecords());
        ok.put("count", page.getTotal());
        return ok;
    }

    @GetMapping("/export")
    @RequiresPermissions("topic:list")
    public R export(@RequestParam Map<String, Object> req, HttpServletResponse response) throws Exception {
        req.put("limit", 100000);
        IPage<TopicResp> page = topicService.listSearch(req);
        List<TopicResp> records = page.getRecords();
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("试题", "excel"), TopicResp.class, records);
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + "exam" + ".xls");
        OutputStream ouputStream = null;
        try {
            ouputStream = response.getOutputStream();
            workbook.write(ouputStream);
            ouputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        } finally {
            if (ouputStream != null) {
                ouputStream.close();
            }
        }
        return null;
    }

    @PostMapping(value = "/import")
    public R excelImport(@RequestBody MultipartFile file) throws Exception {
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        List<TopicResp> objects = ExcelImportUtil.importExcel(file.getInputStream(), TopicResp.class, params);
        SysUserEntity user = this.getUser();
        topicService.saveAll(objects, user.getUserId(), user.getUsername());
        return null;
    }

    /**
     * 查询详情
     * @param id
     * @return R
     */
    @GetMapping("/detail/search")
    @RequiresPermissions("topic:detail:search")
    public R listSearch(@RequestParam Integer id){
        Topic chapter = topicService.detailSearch(id);
        return R.ok().put("data", chapter);
    }

    /**
     * 新增/修改
     * @param req
     * @return R
     */
    @PostMapping("/save")
    @RequiresPermissions("topic:save")
    public R save(@RequestBody TopicModel req){
       // this.verifyForm(req);
//        if (req.getId() == null || 0 == req.getId()) {
//            topicService.save(req);
//        } else {
//            topicService.updateById(req);
//        }
    	SysUserEntity user = this.getUser();
    	int i=topicService.adds(req, user.getUserId(), user.getUsername());
        return R.ok().put("data", i);
    }
    /**
     * 删除
     * @param ids
     * @return R
     * 
     */
    @PostMapping("/delete")
    @RequiresPermissions("topic:status:modify")
    public R delete(@RequestBody Integer[] ids) {
    	
    	topicService.delete(Arrays.asList(ids));
    	return R.ok();
    }
//
//    /**
//     * 批量修改状态
//     * @param req
//     * @return R
//     */
//    @PostMapping("/status/modify")
//    @RequiresPermissions("topic:status:modify")
//    public R statusModify(@RequestBody StatusReqs req){
//        topicService.statusModify(req.getReq());
//        return R.ok();
//    }
//
//    /**
//     * 校验数据
//     * @param chapter
//     * @return
//     */
//    public void verifyForm(Topic topic) {
//        if(StringUtils.isBlank(chapter.getName())){
//            throw new RRException("章节名称不能为空");
//        }
//
//        if(chapter.getCourseId() == null || 0 == chapter.getCourseId()){
//            throw new RRException("课程不能为空");
//        }
//
//        //菜单
//        if(chapter.getOrderNum() == null || chapter.getOrderNum() == 0){
//            throw new RRException("章节编号不能为空");
//        }
//
//        CourseEntity course = courseService.getById(chapter.getCourseId());
//        if (course == null) {
//            throw new RRException("课程不存在");
//        }
//        chapter.setCourseName(course.getName());
//    }
}

